<?php

namespace server\dao;

use common\helpers\BaseHelper;
use Yii;
use yii\db\Query;

/**
 * This is the model class for table "sys_operation_log".
 *
 * @property int    $id
 * @property int    $user_id
 * @property string $user_info  操作人信息,“username-id
 * @property string $app_id     系统id
 * @property string $module     模块名
 * @property string $action     操作id
 * @property string $version    版本
 * @property string $data       操作数据
 * @property string $ip         客户端IP地址
 * @property int    $created_at 创建时间
 */
class SysOperationLog_bak extends \common\core\server\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'sys_operation_log';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_id', 'user_info', 'app_id', 'module', 'action', 'data', 'ip'], 'required'],
            [['created_at'], 'integer'],
            [['data'], 'string'],
            [['user_info', 'app_id', 'module', 'action', 'controller', 'version'], 'string', 'max' => 32],
            [['ip'], 'string', 'max' => 15],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'user_id'    => 'User ID',
            'user_info'  => '操作人信息,“username-id',
            'app_id'     => '系统id',
            'version'    => '版本',
            'module'     => '模块名',
            'controller' => '控制器',
            'action'     => '操作id',
            'data'       => '操作数据',
            'ip'         => '客户端IP地址',
            'created_at' => '创建时间',
        ];
    }

    /**
     * 获得 操作日志
     *
     * @param array $params
     *
     * @return mixed
     */
    public function getOperationLog(array $params)
    {
        $query = new Query();
        $query->from($this->tableName());

        if ($this->issetAndEmpty($params, 'user_info')) {
            $query->andWhere(['like', 'user_info', $params['user_info']]);
        }

        if ($this->issetAndEmpty($params, 'app_id')) {
            $query->andWhere(['like', 'app_id', $params['app_id']]);
        }

        if ($this->issetAndEmpty($params, 'module')) {
            $query->andWhere(['like', 'module', $params['module']]);
        }

        if ($this->issetAndEmpty($params, 'action')) {
            $query->andWhere(['like', 'action', $params['action']]);
        }

        /*if ($this->issetAndEmpty($params, 'created_at')) {
            $time = BaseHelper::formattingDoubleDate($params['created_at']);
            $query->andWhere(['between', 'created_at', $time[0], $time[1]]);
        }

        $this->order($query, 'created_at desc');*/
        if ($this->issetAndEmpty($params, 'startTime')) {
            $query->andWhere(['>=', 'created_at', $params['startTime']]);
        }
        if ($this->issetAndEmpty($params, 'endTime')) {
            $query->andWhere(['<=', 'created_at', $params['endTime']]);
        }
        $query->orderBy('created_at desc');
        return $this->page($query);
    }

    /**
     * 操作日志
     *
     * @param array $params
     *
     * @return mixed
     * @throws
     */
    public function setOperationLog(array $params)
    {
        $log = new SysOperationLog();
        $log->setAttributes($params);
        $log->save();
        return $log;
    }

    /**
     * table name
     *
     * @return mixed
     */
    public function table()
    {
        return '系统操作日志';
    }
}
